﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.Authentication;
using Marbles.Core.Content.Pages;
using Marbles.Core.Pages;

namespace Marbles.Web.Admin.Templates
{
    public partial class Admin : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AuthenticationProvider prov = AuthenticationProvider.CreateProviderInstance();

            cBtnLogout.Visible = prov.IsAuthenticated;
            cBtnHome.Visible = prov.IsAuthenticated;
            cBtnPages.Visible = cBtnHome.Visible;
            cBtnFiles.Visible = cBtnHome.Visible;
            cBtnImages.Visible = cBtnHome.Visible;
            cBtnLinks.Visible = cBtnHome.Visible;
            cBtnUsers.Visible = cBtnHome.Visible;
            cBtnSite.Visible = cBtnHome.Visible;
            cBtnAbout.Visible = cBtnHome.Visible;
            cBtnTemplates.Visible = cBtnHome.Visible;
            cLblTitle.Text = this.Page.Title;

            if (!Page.ClientScript.IsClientScriptIncludeRegistered("App"))
            {
                Page.ClientScript.RegisterClientScriptInclude(typeof(System.String), "App", Resources.ResourceHandler.GetApplicationJavascript(this.Page));
            }

            if (prov.IsAuthenticated)
            {                
                if (!((prov.CurrentToken.IsAdministrator) || (prov.CurrentToken.IsEditor)))
                {
                    prov.Logout();
                    Response.Redirect("/Admin/default.aspx", true);
                }
            }

            if (!Page.IsPostBack)
            {
                if (prov.IsAuthenticated)
                {
                    if (!string.IsNullOrEmpty(Context.Request.Params["page"]))
                    {
                        LoadContent(Context.Request.Params["page"]);
                    }
                    else
                    {
                        LoadContent("ControlPanel", "Administration Console");
                    }

                        __SITE.Value = ((Marbles.Core.Pages.Page)this.Page).CurrentWeb.SiteId;
                }
            }
            else
            {
                if (string.IsNullOrEmpty(__SITE.Value))
                {
                    __SITE.Value = ((Marbles.Core.Pages.Page)this.Page).CurrentWeb.SiteId;
                }

                if (!string.IsNullOrEmpty(__CONTENT.Value)) { LoadContent(); }
            }

        }

        protected void Page_Command(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Home":
                    Response.Redirect("/Admin/Default.aspx", true);
                    break;
                case "Logout":
                    AuthenticationProvider.CreateProviderInstance().Logout();
                    Response.Redirect("/Admin/Default.aspx", true);
                    break;
                default:
                    Response.Redirect("/Admin/Default.aspx?page=" + e.CommandName, true);
                    break;
            }
        }

        public void LoadContent()
        {
            string[] args = __CONTENT.Value.Split('$');
            if (args.Length == 2)
            {
                LoadContent(args[0], args[1]);
            }
            else
            {
                LoadContent("ControlPanel", "Administration Console");
            }
        }

        public void LoadContent(string Name)
        {
            switch (Name)
            {
                case "Site":
                    LoadContent("SiteMgr", "Site Manager");
                    break;
                case "Pages":
                    LoadContent("PageMgr", "Page Manager");
                    break;
                case"Templates":
                    LoadContent("TemplateMgr", "Template Manager");
                    break;
                case "Files":
                    LoadContent("FileMgr", "File Manager");
                    break;
                case "Images":
                    LoadContent("ImgMgr", "Image Manager");
                    break;
                case "Links":
                    LoadContent("LinkMgr", "Favourites Manager");
                    break;
                case "Users":
                    LoadContent("UserMgr", "User Manager");
                    break;
                case "CDN":
                    LoadContent("CDN", "CDN Settings");
                    break;
                case "BBS":
                    LoadContent("BBS", "Forums");
                    break;
                case "BBSCat":
                    LoadContent("BBSCategory", "Forum Categories");
                    break;
                case "System":
                    LoadContent("Config", "Advanced Configuration");
                    break;
                case "Themes":
                    LoadContent("Themes", "Themes");
                    break;
                case "ProfileQuestions":
                    LoadContent("ProfileQuestions", "Profile Questions");
                    break;
                case "Modules":
                    LoadContent("ModuleManager", "Module Manager");
                    break;
                case "Profiles":
                    LoadContent("Profiles", "Profiles");
                    break;
                case"UrlShort":
                    LoadContent("UrlShort", "Url Shortener");
                    break;
            }

        }

        public void LoadContent(string filename, string title)
        {
            AdminPage page = this.Page as AdminPage;
            __CONTENT.Value = filename + "$" + title;
            Control control = page.LoadControl("~/Admin/Content/" + filename + ".ascx");
            cLblTitle.Text = title;
            page.Title = title;
            Body.Controls.Add(control);
            ((Marbles.Web.Admin.Content.ContentBase)control).FirstRun();
        }

    }


}